//
//  MyHistMenuTableViewCell_FlowCollectionBase.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/3/10.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MyHistMenuTableViewCell_FlowCollectionBase.h"
#import "MenuCollectionViewCellModel.h"
@implementation MyHistMenuTableViewCell_FlowCollectionBase
+ (Class)flowLayoutClass{
    return [UICollectionViewFlowLayout class];
}
- (void)configFlowLayout{
    UICollectionViewFlowLayout *flowLayout = self.flowLayout;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 40;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.itemSize = CGSizeMake(60, 60);
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.flowLayout = [[self.class.flowLayoutClass alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        [self configFlowLayout];
        
        [self.cellContentView addSubview:self.collectionView];
        //
        self.model = [[MKUICollectionViewModel alloc] initWithCollectionView:self.collectionView];
        //
        [self colorTraitCollectionDidChange];
    }
    return self;
}

- (void)colorTraitCollectionDidChange{
    [super colorTraitCollectionDidChange];
    self.collectionView.backgroundColor = [UIColor mk_colorWithLight:[UIColor whiteColor]];
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.cellContentView.bounds;
    CGRect f1 = bounds;
    self.collectionView.frame = f1;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    [self __reloadData];
}
- (void)__reloadData{
    [self.model removeAllSectionModels];
    MyMenuInfo *info = self.cellModel.modelValue;
    for (Menu *menu in info.histMenus) {
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        [self configCollectionCellModel:cm];
        [self.model addCellModel:cm];
    }
    [self.model reloadCollectionViewData];
    [self.collectionView layoutIfNeeded];
}
- (void)configCollectionCellModel:(MenuCollectionViewCellModel *)cm{
    cm.cellClass = [MenuCollectionViewCell_FixSize class];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    UIEdgeInsets insets = [self.class contentInsets];
    size.width -= (insets.left+insets.right);

    CGSize s = [self.flowLayout mk_sizeThatFits:CGSizeMake(size.width, 999999)];
    s.height = MIN(300, s.height);
    s.height += insets.top+insets.bottom;
    return s;
}

@end
